home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / appleevent send and receive / source / aerecieve source / aereceiveframe.java < prev   
Encoding:
Java Source  |  2000-06-23  |  6.8 KB  |  263 lines

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. /**
  6.  * Apple Worldwide Developer Technical Support
  7.  *
  8.  * Sample showing how to send and receive AppleEvents using JDirect 2.
  9.  *
  10.  * File: AEReceiveFrame.java
  11.  *
  12.  * This class contains this sample's user interface for receiving AppleEvents.
  13.  *
  14.  * @author Levi Brown
  15.  * @author Apple Computer, Inc.
  16.  *
  17.  * Copyright ©1999 Apple Computer, Inc.
  18.  * All rights reserved.
  19.  *
  20.  * @version 1.0
  21.  * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
  22.  *
  23.  * You may incorporate this sample code into your applications without
  24.  * restriction, though the sample code has been provided "AS IS" and the
  25.  * responsibility for its operation is 100% yours.  However, what you are
  26.  * not permitted to do is to redistribute the source as "Apple Sample
  27.  * Code" after having made changes. If you're going to re-distribute the
  28.  * source, we require that you make it clear in the source that the code
  29.  * was descended from Apple Sample Code, but that you've made changes.
  30.  */
  31. public class AEReceiveFrame extends Frame
  32. {
  33.     public AEReceiveFrame()
  34.     {
  35.         //{{INIT_CONTROLS
  36.         GridBagLayout gridBagLayout;
  37.         gridBagLayout = new GridBagLayout();
  38.         setLayout(gridBagLayout);
  39.         setVisible(false);
  40.         setSize(466,90);
  41.         setBackground(new Color(-1052689));
  42.         label1 = new java.awt.Label("Message:");
  43.         label1.setBounds(5,0,50,21);
  44.         label1.setFont(new Font("SansSerif", Font.BOLD, 9));
  45.         GridBagConstraints gbc;
  46.         gbc = new GridBagConstraints();
  47.         gbc.gridx = 1;
  48.         gbc.gridy = 1;
  49.         gbc.gridwidth = 2;
  50.         gbc.anchor = GridBagConstraints.WEST;
  51.         gbc.fill = GridBagConstraints.NONE;
  52.         gbc.insets = new Insets(0,5,0,0);
  53.         ((GridBagLayout)getLayout()).setConstraints(label1, gbc);
  54.         add(label1);
  55.         label2 = new java.awt.Label("Status:");
  56.         label2.setBounds(5,65,40,21);
  57.         label2.setFont(new Font("SansSerif", Font.BOLD, 9));
  58.         gbc = new GridBagConstraints();
  59.         gbc.gridx = 1;
  60.         gbc.gridy = 5;
  61.         gbc.anchor = GridBagConstraints.WEST;
  62.         gbc.fill = GridBagConstraints.NONE;
  63.         gbc.insets = new Insets(0,5,0,0);
  64.         ((GridBagLayout)getLayout()).setConstraints(label2, gbc);
  65.         add(label2);
  66.         divider1 = new Divider();
  67.         divider1.setBounds(5,64,434,1);
  68.         gbc = new GridBagConstraints();
  69.         gbc.gridx = 1;
  70.         gbc.gridy = 4;
  71.         gbc.gridwidth = 2;
  72.         gbc.weightx = 0.5;
  73.         gbc.anchor = GridBagConstraints.WEST;
  74.         gbc.fill = GridBagConstraints.HORIZONTAL;
  75.         gbc.insets = new Insets(0,5,0,5);
  76.         ((GridBagLayout)getLayout()).setConstraints(divider1, gbc);
  77.         add(divider1);
  78.         statusLabel = new java.awt.Label("Initializiing...");
  79.         statusLabel.setBounds(50,65,394,21);
  80.         statusLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
  81.         gbc = new GridBagConstraints();
  82.         gbc.gridx = 2;
  83.         gbc.gridy = 5;
  84.         gbc.weightx = 0.5;
  85.         gbc.anchor = GridBagConstraints.WEST;
  86.         gbc.fill = GridBagConstraints.HORIZONTAL;
  87.         gbc.insets = new Insets(0,5,0,0);
  88.         ((GridBagLayout)getLayout()).setConstraints(statusLabel, gbc);
  89.         add(statusLabel);
  90.         messageLabel = new java.awt.Label("");
  91.         messageLabel.setBounds(10,21,434,21);
  92.         messageLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
  93.         gbc = new GridBagConstraints();
  94.         gbc.gridx = 1;
  95.         gbc.gridy = 2;
  96.         gbc.gridwidth = 2;
  97.         gbc.weightx = 0.5;
  98.         gbc.anchor = GridBagConstraints.WEST;
  99.         gbc.fill = GridBagConstraints.HORIZONTAL;
  100.         gbc.insets = new Insets(0,10,0,0);
  101.         ((GridBagLayout)getLayout()).setConstraints(messageLabel, gbc);
  102.         add(messageLabel);
  103.         panel1 = new java.awt.Panel();
  104.         panel1.setLayout(null);
  105.         panel1.setBounds(0,42,444,22);
  106.         gbc = new GridBagConstraints();
  107.         gbc.gridx = 1;
  108.         gbc.gridy = 3;
  109.         gbc.gridwidth = 2;
  110.         gbc.weightx = 0.5;
  111.         gbc.weighty = 0.5;
  112.         gbc.fill = GridBagConstraints.BOTH;
  113.         gbc.insets = new Insets(0,0,0,0);
  114.         ((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
  115.         add(panel1);
  116.         setTitle("AEReceive");
  117.         //}}
  118.  
  119.         //{{INIT_MENUS
  120.         //}}
  121.  
  122.         //{{REGISTER_LISTENERS
  123.         SymWindow aSymWindow = new SymWindow();
  124.         this.addWindowListener(aSymWindow);
  125.         //}}
  126.  
  127.         setLocation(50, 180);
  128.  
  129.         //Create a new timer to handle status messages.
  130.         timer = new Timer(kEraseSleep, new TimerCallback()
  131.                 {
  132.                     public void timeIsUp()
  133.                     {
  134.                         isErase = false;
  135.                         try
  136.                         {
  137.                             setStatus("Idle");
  138.                         }
  139.                         finally
  140.                         {
  141.                             isErase = true;
  142.                         }
  143.                     }
  144.                 });
  145.         
  146.         isErase = true;
  147.         
  148.         //Create a new AEReceive object so we can register out AppleEvent handler
  149.         aeReceive = new AEReceive();
  150.         //Register a new instance of our ActionListener with the AEReceive object
  151.         //so we get notified when an AppleEvent is received.
  152.         aeReceive.addActionListener(new Action());
  153.     }
  154.     
  155.     /**
  156.      * The entry point to our application
  157.      */    
  158.     public static void main(String[] args)
  159.     {
  160.         (new AEReceiveFrame()).setVisible(true);
  161.     }
  162.  
  163.     /**
  164.      * Gets called by the framework when this component is added to the component hierarchy.
  165.      * Overriden here to install our AppleEvent handler.
  166.      */
  167.     public void addNotify()
  168.     {
  169.         super.addNotify();
  170.         
  171.         setStatus("Installing AppleEvent handler...");
  172.         try
  173.         {
  174.             aeReceive.installAEHandler();
  175.         }
  176.         catch (NativeException exc)
  177.         {
  178.             setStatus("Failed to install AppleEvent handler. (#" + exc.getErrNum() + ")");
  179.             return;
  180.         }
  181.  
  182.         setStatus("AppleEvent handler successfully installed.");
  183.     }
  184.  
  185.     /**
  186.      * Set the text of the status label
  187.      * @param the message to use.
  188.      */
  189.     protected void setStatus(String message)
  190.     {
  191.         statusLabel.setText(message);
  192.         if (isErase)
  193.             timer.start();
  194.     }
  195.     
  196.     /**
  197.      * Set the text of the message label
  198.      * @param the message to use.
  199.      */
  200.     protected void setMessage(String message)
  201.     {
  202.         messageLabel.setText(message);
  203.     }
  204.  
  205.     //{{DECLARE_CONTROLS
  206.     java.awt.Label label1;
  207.     java.awt.Label label2;
  208.     Divider divider1;
  209.     java.awt.Label statusLabel;
  210.     java.awt.Label messageLabel;
  211.     java.awt.Panel panel1;
  212.     //}}
  213.  
  214.     //{{DECLARE_MENUS
  215.     //}}
  216.  
  217.     class SymWindow extends java.awt.event.WindowAdapter
  218.     {
  219.         public void windowClosing(java.awt.event.WindowEvent event)
  220.         {
  221.             Object object = event.getSource();
  222.             if (object == AEReceiveFrame.this)
  223.                 AEReceiveFrame_WindowClosing(event);
  224.         }
  225.     }
  226.     
  227.     void AEReceiveFrame_WindowClosing(java.awt.event.WindowEvent event)
  228.     {
  229.         setVisible(false);         // hide the AEReceiveFrame
  230.         dispose();            // free the system resources
  231.         System.exit(0);        // close the application
  232.     }
  233.  
  234.     /**
  235.      * An inner class designed to listen for ActionEvents
  236.      * from the AEReceive class.
  237.      */    
  238.     class Action implements ActionListener
  239.     {
  240.         public void actionPerformed(ActionEvent event)
  241.         {
  242.             setMessage(event.getActionCommand());
  243.             setStatus("Received AppleEvent.");
  244.         }
  245.     }
  246.  
  247.     /**
  248.      * The time in milliseconds to wait before removing the previous status message.
  249.      */
  250.     protected static final int kEraseSleep = 1250;
  251.     /**
  252.      * Our AEReceive object which allows us to register the AppleEvent Handler
  253.      */
  254.     protected AEReceive aeReceive;
  255.     /**
  256.      * An internal flag to prevent recursion in the status message handling code.
  257.      */
  258.     protected boolean isErase;
  259.     /**
  260.      * A timer to keep track of displaying the status message.
  261.      */
  262.     protected Timer timer;
  263. }